package com.xjlsoft.weather.service.impl;

import com.xjlsoft.weather.dto.ExportDto;
import com.xjlsoft.weather.dto.GroundDataListDto;
import com.xjlsoft.weather.dto.GroundPreListDto;
import com.xjlsoft.weather.dto.WeatherDataQueryDto;
import com.xjlsoft.weather.entity.GroundData;
import com.xjlsoft.weather.entity.GroundPre;
import com.xjlsoft.weather.mapper.GroundDataMapper;
import com.xjlsoft.weather.mapper.GroundPreMapper;
import com.xjlsoft.weather.service.GroundPreService;
import com.xjlsoft.weather.util.WeatherDataUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地面数据 降水资料 服务实现类
 * </p>
 *
 * @author zhangbanglong
 * @since 2020-08-28
 */
@Service
public class GroundPreServiceImpl extends ServiceImpl<GroundPreMapper, GroundPre> implements GroundPreService {

	
	@Autowired
	private GroundPreMapper groundPreMapper;
	@Override
	public GroundPre getGroundPreByid(String groundDataId) {
		GroundPre  gd=groundPreMapper.selectById(groundDataId);
		return gd;
	}

	@Override
	public IPage<GroundPreListDto> getList(Page page, WeatherDataQueryDto weatherDataDto) throws Exception {
		QueryWrapper<GroundData> query = Wrappers.query();
       
		if(StringUtils.isNotBlank(weatherDataDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(weatherDataDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(weatherDataDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(weatherDataDto.getDateEnd());
			query.le("data_time", timeend);
		}
        IPage<GroundPreListDto> list =groundPreMapper.getGroundPresByParams(page, query);
        		
        		
		return list;
	}

	@Override
	public List<GroundPre> getList(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundPre> query = Wrappers.query();
		
    
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
       
        List<GroundPre> list =groundPreMapper.selectList(query);
        		//groundDataMaper.selectPage(page, query);
		return list;
	}

	@Override
	public List<Map<String, Object>> getListMap(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundPre> query = Wrappers.query();
		query.select(exportDto.getSelectFilds());
	
		
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		 List<Map<String, Object>> map= groundPreMapper.selectMaps(query);	
	     return map;
	}

	@Override
	public int getListMapCount(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundPre> query = Wrappers.query();
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		int count=groundPreMapper.selectCount(query);
		return count;
	}

}
